home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 16 / CU Amiga Magazine's Super CD-ROM 16 (1997-10-16)(EMAP Images)(GB)[!][issue 1997-11].iso / CUCD / Graphics / Ghostscript / source / gspcolor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-23  |  25.1 KB  |  795 lines

  1. /* Copyright (C) 1993, 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gspcolor.c */
  20. /* Pattern color operators and procedures for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsrop.h"
  25. #include "gsstruct.h"
  26. #include "gsutil.h"            /* for gs_next_ids */
  27. #include "gxarith.h"
  28. #include "gxfixed.h"
  29. #include "gxmatrix.h"
  30. #include "gxcoord.h"            /* for gs_concat, gx_tr'_to_fixed */
  31. #include "gxcspace.h"            /* for gscolor2.h */
  32. #include "gxcolor2.h"
  33. #include "gxdcolor.h"
  34. #include "gxdevice.h"
  35. #include "gxdevmem.h"
  36. #include "gxclip2.h"
  37. #include "gspath.h"
  38. #include "gxpath.h"
  39. #include "gxpcolor.h"
  40. #include "gzstate.h"
  41. #include "gsimage.h"
  42.  
  43. private_st_client_pattern();
  44. public_st_pattern_instance();
  45.  
  46. /* Import the Pattern reloading procedure from gxpcmap.c. */
  47. int gx_pattern_load(P4(gx_device_color *, const gs_imager_state *,
  48.                gx_device *, gs_color_select_t));
  49.  
  50. /* Define the Pattern color space. */
  51. extern cs_proc_remap_color(gx_remap_Pattern);
  52. private cs_proc_install_cspace(gx_install_Pattern);
  53. private cs_proc_adjust_cspace_count(gx_adjust_cspace_Pattern);
  54. private cs_proc_init_color(gx_init_Pattern);
  55. private cs_proc_adjust_color_count(gx_adjust_color_Pattern);
  56. private struct_proc_enum_ptrs(gx_enum_ptrs_Pattern);
  57. private struct_proc_reloc_ptrs(gx_reloc_ptrs_Pattern);
  58. const gs_color_space_type
  59.     gs_color_space_type_Pattern =
  60.      { gs_color_space_index_Pattern, -1, false,
  61.        gx_init_Pattern, gx_no_concrete_space,
  62.        gx_no_concretize_color, NULL,
  63.        gx_remap_Pattern, gx_install_Pattern,
  64.        gx_adjust_cspace_Pattern, gx_adjust_color_Pattern,
  65.        gx_enum_ptrs_Pattern, gx_reloc_ptrs_Pattern
  66.      };
  67.  
  68. /* makepattern */
  69. private int compute_inst_matrix(P3(gs_pattern_instance *pinst,
  70.                    const gs_state *saved,
  71.                    gs_rect *pbbox));
  72. int
  73. gs_makepattern(gs_client_color *pcc, const gs_client_pattern *pcp,
  74.   const gs_matrix *pmat, gs_state *pgs, gs_memory_t *mem)
  75. {    gs_pattern_instance inst;
  76.     gs_pattern_instance *pinst;
  77.     gs_state *saved;
  78.     gs_rect bbox;
  79.     gs_fixed_rect cbox;
  80.     int code;
  81.  
  82.     if ( mem == 0 )
  83.       mem = pgs->memory;
  84.     rc_alloc_struct_1(pinst, gs_pattern_instance, &st_pattern_instance,
  85.               mem, return_error(gs_error_VMerror),
  86.               "gs_makepattern");
  87.     inst.rc = pinst->rc;
  88.     saved = gs_state_copy(pgs, mem);
  89.     if ( saved == 0 )
  90.       { code = gs_note_error(gs_error_VMerror);
  91.         goto finst;
  92.       }
  93.     gs_concat(saved, pmat);
  94.     gs_newpath(saved);
  95.     switch ( pcp->PaintType )
  96.       {
  97.       case 1:        /* colored */
  98.         gs_set_logical_op(saved, lop_default);
  99.         break;
  100.       case 2:        /* uncolored */
  101.         gx_set_device_color_1(saved);
  102.         break;
  103.       default:
  104.         code = gs_note_error(gs_error_rangecheck);
  105.         goto fsaved;
  106.       }
  107.     inst.template = *pcp;
  108.     inst.saved = saved;
  109.     code = compute_inst_matrix(&inst, saved, &bbox);
  110.     if ( code < 0 )
  111.       goto fsaved;
  112. #define mat inst.step_matrix
  113.     if_debug6('t', "[t]step_matrix=[%g %g %g %g %g %g]\n",
  114.           mat.xx, mat.xy, mat.yx, mat.yy, mat.tx, mat.ty);
  115.     /* Check for singular stepping matrix. */
  116.     if ( fabs(mat.xx * mat.yy - mat.xy * mat.yx) < 1.0e-6 )
  117.       { code = gs_note_error(gs_error_rangecheck);
  118.         goto fsaved;
  119.       }
  120.     if_debug4('t', "[t]bbox=(%g,%g),(%g,%g)\n",
  121.           bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y);
  122.     {    float bbw = bbox.q.x - bbox.p.x;
  123.         float bbh = bbox.q.y - bbox.p.y;
  124.         /* If the step and the size agree to within 1/2 pixel, */
  125.         /* make them the same. */
  126.         inst.size.x = (int)(bbw + 0.8);    /* 0.8 is arbitrary */
  127.         inst.size.y = (int)(bbh + 0.8);
  128.         if ( mat.xy == 0 && mat.yx == 0 &&
  129.              fabs(fabs(mat.xx) - bbw) < 0.5 &&
  130.              fabs(fabs(mat.yy) - bbh) < 0.5
  131.            )
  132.           {    gs_scale(saved, fabs(inst.size.x / mat.xx),
  133.                  fabs(inst.size.y / mat.yy));
  134.             code = compute_inst_matrix(&inst, saved, &bbox);
  135.             if ( code < 0 )
  136.               goto fsaved;
  137.             if_debug2('t',
  138.                 "[t]adjusted XStep & YStep to size=(%d,%d)\n",
  139.                 inst.size.x, inst.size.y);
  140.             if_debug4('t', "[t]bbox=(%g,%g),(%g,%g)\n",
  141.                   bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y);
  142.           }
  143.     }
  144.     if ( (code = gs_bbox_transform_inverse(&bbox, &mat, &inst.bbox)) < 0 )
  145.       goto fsaved;
  146.     if_debug4('t', "[t]ibbox=(%g,%g),(%g,%g)\n",
  147.           inst.bbox.p.x, inst.bbox.p.y, inst.bbox.q.x, inst.bbox.q.y);
  148.     inst.is_simple = (fabs(mat.xx) == inst.size.x && mat.xy == 0 &&
  149.               mat.yx == 0 && fabs(mat.yy) == inst.size.y);
  150.     if_debug6('t',
  151.           "[t]is_simple? xstep=(%g,%g) ystep=(%g,%g) size=(%d,%d)\n",
  152.           inst.step_matrix.xx, inst.step_matrix.xy,
  153.           inst.step_matrix.yx, inst.step_matrix.yy,
  154.           inst.size.x, inst.size.y);
  155.     gx_translate_to_fixed(saved, float2fixed(mat.tx - bbox.p.x),
  156.                   float2fixed(mat.ty - bbox.p.y));
  157.     mat.tx = bbox.p.x;
  158.     mat.ty = bbox.p.y;
  159. #undef mat
  160.     cbox.p.x = fixed_0;
  161.     cbox.p.y = fixed_0;
  162.     cbox.q.x = int2fixed(inst.size.x);
  163.     cbox.q.y = int2fixed(inst.size.y);
  164.     code = gx_clip_to_rectangle(saved, &cbox);
  165.     if ( code < 0 )
  166.       goto fsaved;
  167.     inst.id = gs_next_ids(1);
  168.     *pinst = inst;
  169.     pcc->pattern = pinst;
  170.     return 0;
  171. #undef mat
  172. fsaved:    gs_state_free(saved);
  173. finst:    gs_free_object(mem, pinst, "gs_makepattern");
  174.     return code;
  175. }
  176. /* Compute the stepping matrix and device space instance bounding box */
  177. /* from the step values and the saved matrix. */
  178. private int
  179. compute_inst_matrix(gs_pattern_instance *pinst, const gs_state *saved,
  180.   gs_rect *pbbox)
  181. {    double xx = pinst->template.XStep * saved->ctm.xx;
  182.     double xy = pinst->template.XStep * saved->ctm.xy;
  183.     double yx = pinst->template.YStep * saved->ctm.yx;
  184.     double yy = pinst->template.YStep * saved->ctm.yy;
  185.  
  186.     /* Adjust the stepping matrix so all coefficients are >= 0. */
  187.     if ( xx == 0 || yy == 0 )
  188.       { /* We know that both xy and yx are non-zero. */
  189.         double temp;
  190.         temp = xx, xx = yx, yx = temp;
  191.         temp = xy, xy = yy, yy = temp;
  192.       }
  193.     if ( xx < 0 )
  194.       xx = -xx, xy = -xy;
  195.     if ( yy < 0 )
  196.       yx = -yx, yy = -yy;
  197.     /* Now xx > 0, yy > 0. */
  198.     pinst->step_matrix.xx = xx;
  199.     pinst->step_matrix.xy = xy;
  200.     pinst->step_matrix.yx = yx;
  201.     pinst->step_matrix.yy = yy;
  202.     pinst->step_matrix.tx = saved->ctm.tx;
  203.     pinst->step_matrix.ty = saved->ctm.ty;
  204.     return gs_bbox_transform(&pinst->template.BBox, &ctm_only(saved),
  205.                  pbbox);
  206. }
  207.  
  208. /* setpattern */
  209. int
  210. gs_setpattern(gs_state *pgs, const gs_client_color *pcc)
  211. {    int code = gs_setpatternspace(pgs);
  212.     if ( code < 0 )
  213.         return code;
  214.     return gs_setcolor(pgs, pcc);
  215. }
  216.  
  217. /* setpatternspace */
  218. /* This does all the work of setpattern except for the final setcolor. */
  219. int
  220. gs_setpatternspace(gs_state *pgs)
  221. {    int code = 0;
  222.     if ( pgs->color_space->type->index != gs_color_space_index_Pattern )
  223.     {    gs_color_space cs;
  224.         cs.params.pattern.base_space =
  225.             *(gs_paint_color_space *)pgs->color_space;
  226.         cs.params.pattern.has_base_space = true;
  227.         cs.type = &gs_color_space_type_Pattern;
  228.         code = gs_setcolorspace(pgs, &cs);
  229.     }
  230.     return code;
  231. }
  232.  
  233. /* getpattern */
  234. /* This is only intended for the benefit of pattern PaintProcs. */
  235. const gs_client_pattern *
  236. gs_getpattern(const gs_client_color *pcc)
  237. {    return &pcc->pattern->template;
  238. }
  239.  
  240. /* makebitmappattern */
  241. private int bitmap_PaintProc(P2(const gs_client_color *, gs_state *));
  242. int
  243. gs_makebitmappattern(gs_client_color *pcc, const gx_tile_bitmap *tile,
  244.   bool mask, gs_state *pgs, gs_memory_t *mem)
  245. {    gs_client_pattern pat;
  246.     gs_matrix mat, smat;
  247.  
  248.     if ( tile->raster != bitmap_raster(tile->size.x) )
  249.       return_error(gs_error_rangecheck);
  250.     uid_set_UniqueID(&pat.uid, gs_next_ids(1));
  251.     pat.PaintType = (mask ? 2 : 1);
  252.     pat.TilingType = 1;
  253.     pat.BBox.p.x = 0;
  254.     pat.BBox.p.y = 0;
  255.     pat.BBox.q.x = tile->size.x;
  256.     pat.BBox.q.y = tile->rep_height;
  257.     pat.XStep = tile->size.x;
  258.     pat.YStep = tile->rep_height;
  259.     pat.PaintProc = bitmap_PaintProc;
  260.     pat.client_data = tile->data;
  261.     gs_make_identity(&mat);
  262.     gs_currentmatrix(pgs, &smat);
  263.     if ( smat.yy > 0 )
  264.       mat.yy = -1;
  265.     gs_setmatrix(pgs, &mat);
  266.     gs_makepattern(pcc, &pat, &mat, pgs, mem);
  267.     gs_setmatrix(pgs, &smat);
  268.     return 0;
  269. }
  270. private int
  271. bitmap_PaintProc(const gs_client_color *pcolor, gs_state *pgs)
  272. {    gs_image_enum *pen =
  273.       gs_image_enum_alloc(gs_state_memory(pgs), "bitmap_PaintProc");
  274.     const gs_client_pattern *ppat = gs_getpattern(pcolor);
  275.     const byte *dp = ppat->client_data;
  276.     gs_image_t image;
  277.     int n;
  278.     uint nbytes, raster, used;
  279.  
  280.     if ( ppat->PaintType == 2 )
  281.       gs_image_t_init_mask(&image, true);
  282.     else
  283.       { gs_image_t_init_gray(&image);
  284.         image.Decode[0] = 1.0;
  285.         image.Decode[1] = 0.0;
  286.       }
  287.     image.Width = (int)ppat->XStep;
  288.     image.Height = (int)ppat->YStep;
  289.     raster = bitmap_raster(image.Width);
  290.     nbytes = (image.Width + 7) >> 3;
  291.     gs_image_init(pen, &image, false, pgs);
  292.     for ( n = image.Height; n > 0; dp += raster, --n )
  293.       gs_image_next(pen, dp, nbytes, &used);
  294.     gs_image_cleanup(pen);
  295.     gs_free_object(gs_state_memory(pgs), pen, "bitmap_PaintProc");
  296.     return 0;
  297. }
  298.  
  299. /* ------ Color space implementation ------ */
  300.  
  301. /* Pattern device color types. */
  302. /* We need a masked analogue of each of the non-pattern types, */
  303. /* to handle uncolored patterns. */
  304. /* We use 'masked_fill_rect' instead of 'masked_fill_rectangle' */
  305. /* in order to limit identifier lengths to 32 characters. */
  306. private dev_color_proc_load(gx_dc_pattern_load);
  307. private dev_color_proc_fill_rectangle(gx_dc_pattern_fill_rectangle);
  308. private struct_proc_enum_ptrs(dc_pattern_enum_ptrs);
  309. private struct_proc_reloc_ptrs(dc_pattern_reloc_ptrs);
  310. private dev_color_proc_load(gx_dc_pure_masked_load);
  311. private dev_color_proc_fill_rectangle(gx_dc_pure_masked_fill_rect);
  312. private struct_proc_enum_ptrs(dc_masked_enum_ptrs);
  313. private struct_proc_reloc_ptrs(dc_masked_reloc_ptrs);
  314. private dev_color_proc_load(gx_dc_binary_masked_load);
  315. private dev_color_proc_fill_rectangle(gx_dc_binary_masked_fill_rect);
  316. private struct_proc_enum_ptrs(dc_binary_masked_enum_ptrs);
  317. private struct_proc_reloc_ptrs(dc_binary_masked_reloc_ptrs);
  318. private dev_color_proc_load(gx_dc_colored_masked_load);
  319. private dev_color_proc_fill_rectangle(gx_dc_colored_masked_fill_rect);
  320. /* The device color types are exported for gxpcmap.c. */
  321. const gx_device_color_procs
  322.   gx_dc_pattern =
  323.     { gx_dc_pattern_load, gx_dc_pattern_fill_rectangle,
  324.       gx_dc_default_fill_masked,
  325.       dc_pattern_enum_ptrs, dc_pattern_reloc_ptrs
  326.     },
  327.   gx_dc_pure_masked =
  328.     { gx_dc_pure_masked_load, gx_dc_pure_masked_fill_rect,
  329.       gx_dc_default_fill_masked,
  330.       dc_masked_enum_ptrs, dc_masked_reloc_ptrs
  331.     },
  332.   gx_dc_binary_masked =
  333.     { gx_dc_binary_masked_load, gx_dc_binary_masked_fill_rect,
  334.       gx_dc_default_fill_masked,
  335.       dc_binary_masked_enum_ptrs, dc_binary_masked_reloc_ptrs
  336.     },
  337.   gx_dc_colored_masked =
  338.     { gx_dc_colored_masked_load, gx_dc_colored_masked_fill_rect,
  339.       gx_dc_default_fill_masked,
  340.       dc_masked_enum_ptrs, dc_masked_reloc_ptrs
  341.     };
  342. #undef gx_dc_type_pattern
  343. const gx_device_color_procs _ds *gx_dc_type_pattern = &gx_dc_pattern;
  344. #define gx_dc_type_pattern (&gx_dc_pattern)
  345. /* GC procedures */
  346. #define cptr ((gx_device_color *)vptr)
  347. private ENUM_PTRS_BEGIN(dc_pattern_enum_ptrs) {
  348.     return dc_masked_enum_ptrs(vptr, size, index - 1, pep);
  349.     }
  350.     case 0:
  351.     {    gx_color_tile *tile = cptr->colors.pattern.p_tile;
  352.         ENUM_RETURN((tile == 0 ? tile : tile - tile->index));
  353.     }
  354. ENUM_PTRS_END
  355. private RELOC_PTRS_BEGIN(dc_pattern_reloc_ptrs) {
  356.     gx_color_tile *tile = cptr->colors.pattern.p_tile;
  357.     if ( tile != 0 )
  358.       {    uint index = tile->index;
  359.         RELOC_TYPED_OFFSET_PTR(gx_device_color, colors.pattern.p_tile, index);
  360.       }
  361.     dc_masked_reloc_ptrs(vptr, size, gcst);
  362. } RELOC_PTRS_END
  363. private ENUM_PTRS_BEGIN(dc_masked_enum_ptrs) ENUM_SUPER(gx_device_color, st_client_color, mask.ccolor, 1);
  364.     case 0:
  365.     {    gx_color_tile *mask = cptr->mask.m_tile;
  366.         ENUM_RETURN((mask == 0 ? mask : mask - mask->index));
  367.     }
  368. ENUM_PTRS_END
  369. private RELOC_PTRS_BEGIN(dc_masked_reloc_ptrs) {
  370.     gx_color_tile *mask = cptr->mask.m_tile;
  371.     RELOC_SUPER(gx_device_color, st_client_color, mask.ccolor);
  372.     if ( mask != 0 )
  373.       {    uint index = mask->index;
  374.         RELOC_TYPED_OFFSET_PTR(gx_device_color, mask.m_tile, index);
  375.       }
  376. } RELOC_PTRS_END
  377. private ENUM_PTRS_BEGIN(dc_binary_masked_enum_ptrs) {
  378.     return (*gx_dc_procs_ht_binary.enum_ptrs)(vptr, size, index - 2, pep);
  379.     }
  380.     case 0:
  381.     case 1:
  382.       return dc_masked_enum_ptrs(vptr, size, index, pep);
  383. ENUM_PTRS_END
  384. private RELOC_PTRS_BEGIN(dc_binary_masked_reloc_ptrs) {
  385.     dc_masked_reloc_ptrs(vptr, size, gcst);
  386.     (*gx_dc_procs_ht_binary.reloc_ptrs)(vptr, size, gcst);
  387. } RELOC_PTRS_END
  388. #undef cptr
  389.  
  390. /* Macros for pattern loading */
  391. #define FINISH_PATTERN_LOAD\
  392.     while ( !gx_pattern_cache_lookup(pdevc, pis, dev, select) )\
  393.      { code = gx_pattern_load(pdevc, pis, dev, select);\
  394.        if ( code < 0 ) break;\
  395.      }\
  396.     return code;
  397.  
  398. /* Ensure that a colored Pattern is loaded in the cache. */
  399. private int
  400. gx_dc_pattern_load(gx_device_color *pdevc, const gs_imager_state *pis,
  401.   gx_device *dev, gs_color_select_t select)
  402. {    int code = 0;
  403.     FINISH_PATTERN_LOAD
  404. }
  405. /* Ensure that an uncolored Pattern is loaded in the cache. */
  406. private int
  407. gx_dc_pure_masked_load(gx_device_color *pdevc, const gs_imager_state *pis,
  408.   gx_device *dev, gs_color_select_t select)
  409. {    int code = (*gx_dc_procs_pure.load)(pdevc, pis, dev, select);
  410.     if ( code < 0 )
  411.       return code;
  412.     FINISH_PATTERN_LOAD
  413. }
  414. private int
  415. gx_dc_binary_masked_load(gx_device_color *pdevc, const gs_imager_state *pis,
  416.   gx_device *dev, gs_color_select_t select)
  417. {    int code = (*gx_dc_procs_ht_binary.load)(pdevc, pis, dev, select);
  418.     if ( code < 0 )
  419.       return code;
  420.     FINISH_PATTERN_LOAD
  421. }
  422. private int
  423. gx_dc_colored_masked_load(gx_device_color *pdevc, const gs_imager_state *pis,
  424.   gx_device *dev, gs_color_select_t select)
  425. {    int code = (*gx_dc_procs_ht_colored.load)(pdevc, pis, dev, select);
  426.     if ( code < 0 )
  427.       return code;
  428.     FINISH_PATTERN_LOAD
  429. }
  430.  
  431. /* Look up a pattern color in the cache. */
  432. bool
  433. gx_pattern_cache_lookup(gx_device_color *pdevc, const gs_imager_state *pis,
  434.   gx_device *dev, gs_color_select_t select)
  435. {    gx_pattern_cache *pcache = pis->pattern_cache;
  436.     gx_bitmap_id id = pdevc->mask.id;
  437.  
  438.     if ( id == gx_no_bitmap_id )
  439.       {    color_set_null_pattern(pdevc);
  440.         return true;
  441.       }
  442.     if ( pcache != 0 )
  443.       { gx_color_tile *ctile = &pcache->tiles[id % pcache->num_tiles];
  444.         if ( ctile->id == id &&
  445.         (pdevc->type != &gx_dc_pattern ||
  446.          ctile->depth == dev->color_info.depth)
  447.            )
  448.           { if ( pdevc->type == &gx_dc_pattern ) /* colored */
  449.           { pdevc->colors.pattern.p_tile = ctile;
  450.             color_set_phase_mod(pdevc, pis->screen_phase[select].x,
  451.                     pis->screen_phase[select].y,
  452.                     ctile->tbits.rep_width,
  453.                     ctile->tbits.rep_height);
  454.           }
  455.         pdevc->mask.m_tile =
  456.           (ctile->tmask.data == 0 ? (gx_color_tile *)0 :
  457.            ctile);
  458.         return true;
  459.           }
  460.       }
  461.     return false;
  462. }
  463.  
  464. #undef FINISH_PATTERN_LOAD
  465.  
  466. /*
  467.  * The filling procedures below disregard the phase in the color.
  468.  * We don't see how this can possibly work, but it does; and if we
  469.  * do take the phase into account, we get incorrect output.
  470.  */
  471.  
  472. /* Macros for filling with a possibly masked pattern. */
  473. #define BEGIN_PATTERN_FILL\
  474.       {    gx_device_tile_clip cdev;\
  475.         gx_device *pcdev;\
  476.         const gx_strip_bitmap *tmask;\
  477.         int code;\
  478. \
  479.         if ( pdevc->mask.m_tile == 0 )    { /* no clipping */\
  480.           code = 0;\
  481.           pcdev = dev;\
  482.         } else {\
  483.           tmask = &pdevc->mask.m_tile->tmask;\
  484.           code = tile_clip_initialize(&cdev, tmask, dev, 0, 0);\
  485.           if ( code < 0 )\
  486.             return code;\
  487.           pcdev = (gx_device *)&cdev;\
  488.         }
  489. #define CLIPPING_FILL (pcdev == (gx_device *)&cdev)
  490. #define END_PATTERN_FILL\
  491.         return code;\
  492.       }
  493.  
  494. /* Macros for filling with non-standard X and Y stepping. */
  495. /* Free variables: x, y, w, h, ptile. */
  496. /* tbits_or_tmask is whichever of tbits and tmask is supplying */
  497. /* the tile size. */
  498. /* This implementation could be sped up considerably! */
  499. #define BEGIN_STEPS(tbits_or_tmask)\
  500.       {    gs_rect bbox, ibbox;\
  501.         gs_point offset;\
  502.         int x0 = x, x1 = x + w, y0 = y, y1 = y + h;\
  503.         int w0 = w, h0 = h;\
  504.         int i0, i1, j0, j1, i, j;\
  505. \
  506.         bbox.p.x = x0, bbox.p.y = y0;\
  507.         bbox.q.x = x1, bbox.q.y = y1;\
  508.         gs_bbox_transform_inverse(&bbox, &ptile->step_matrix, &ibbox);\
  509.         if_debug10('T',\
  510.               "[T]x,y=(%d,%d) w,h=(%d,%d) => (%g,%g),(%g,%g), offset=(%g,%g)\n",\
  511.               x, y, w, h,\
  512.               ibbox.p.x, ibbox.p.y, ibbox.q.x, ibbox.q.y,\
  513.               ptile->step_matrix.tx, ptile->step_matrix.ty);\
  514.         offset.x = ptile->step_matrix.tx;\
  515.         offset.y = ptile->step_matrix.ty;\
  516.         i0 = (int)floor(ibbox.p.x - ptile->bbox.q.x + 0.000001);\
  517.         i1 = (int)ceil(ibbox.q.x - ptile->bbox.p.x - 0.000001);\
  518.         j0 = (int)floor(ibbox.p.y - ptile->bbox.q.y + 0.000001);\
  519.         j1 = (int)ceil(ibbox.q.y - ptile->bbox.p.y - 0.000001);\
  520.         if_debug4('T', "[T]i=(%d,%d) j=(%d,%d)\n", i0, i1, j0, j1);\
  521.         for ( i = i0; i < i1; i++ )\
  522.           for ( j = j0; j < j1; j++ )\
  523.           {    int xoff, yoff;\
  524.             x = (int)(ptile->step_matrix.xx * i +\
  525.                   ptile->step_matrix.yx * j + offset.x);\
  526.             y = (int)(ptile->step_matrix.xy * i +\
  527.                   ptile->step_matrix.yy * j + offset.y);\
  528.             if_debug4('T', "[T]i=%d j=%d x,y=(%d,%d)", i, j, x, y);\
  529.             w = ptile->tbits_or_tmask.size.x;\
  530.             h = ptile->tbits_or_tmask.size.y;\
  531.             if ( x < x0 ) xoff = x0 - x, x = x0, w -= xoff;\
  532.             else xoff = 0;\
  533.             if ( y < y0 ) yoff = y0 - y, y = y0, h -= yoff;\
  534.             else yoff = 0;\
  535.             if ( x + w > x1 ) w = x1 - x;\
  536.             if ( y + h > y1 ) h = y1 - y;\
  537.             if_debug4('T', "=>(%d,%d) w,h=(%d,%d)\n",\
  538.                   x, y, w, h);\
  539.             if ( w > 0 && h > 0 )\
  540.               {    if ( CLIPPING_FILL )\
  541.                   tile_clip_set_phase(&cdev,\
  542.                               imod(xoff - x, tmask->rep_width),\
  543.                               imod(yoff - y, tmask->rep_height))
  544. #define SOURCE_STEP(src)\
  545.                 (src).sdata = source->sdata + (y - y0) * source->sraster;\
  546.                 (src).sourcex = source->sourcex + (x - x0);\
  547.                 (src).sraster = source->sraster;\
  548.                 (src).id = (w == w0 && h == h0 ?\
  549.                         source->id : gx_no_bitmap_id);\
  550.                 (src).scolors[0] = source->scolors[0];\
  551.                 (src).scolors[1] = source->scolors[1];\
  552.                 (src).use_scolors = source->use_scolors
  553. #define END_STEPS\
  554.               }\
  555.           }\
  556.       }
  557.  
  558. /* Fill a rectangle with a colored Pattern. */
  559. /* Note that we treat this as "texture" for RasterOp. */
  560. private int
  561. gx_dc_pattern_fill_rectangle(const gx_device_color *pdevc, int x, int y,
  562.   int w, int h, gx_device *dev, gs_logical_operation_t lop,
  563.   const gx_rop_source_t *source)
  564. {    gx_color_tile *ptile = pdevc->colors.pattern.p_tile;
  565.     const gx_rop_source_t *rop_source = source;
  566.     gx_rop_source_t no_source;
  567.     gx_strip_bitmap *bits;
  568.  
  569.     if ( ptile == 0 )    /* null pattern */
  570.       return 0;
  571.     if ( rop_source == NULL )
  572.       set_rop_no_source(rop_source, no_source, dev);
  573.     bits = &ptile->tbits;
  574.     BEGIN_PATTERN_FILL
  575.     if ( ptile->is_simple )
  576.       {    int px = imod(-(int)(ptile->step_matrix.tx + 0.5),
  577.                   bits->rep_width);
  578.           int py = imod(-(int)(ptile->step_matrix.ty + 0.5),
  579.                   bits->rep_height);
  580.  
  581.         if ( pcdev != dev )
  582.           tile_clip_set_phase(&cdev, px, py);
  583.         if ( source == NULL && lop_no_S_is_T(lop) )
  584.           code = (*dev_proc(pcdev, strip_tile_rectangle))(pcdev, bits,
  585.             x, y, w, h,
  586.             gx_no_color_index, gx_no_color_index, px, py);
  587.         else
  588.           code = (*dev_proc(dev, strip_copy_rop))(dev,
  589.             rop_source->sdata, rop_source->sourcex,
  590.             rop_source->sraster, rop_source->id,
  591.             (rop_source->use_scolors ? rop_source->scolors : NULL),
  592.             bits, NULL, x, y, w, h, px, py, lop);
  593.       }
  594.     else
  595.       {    BEGIN_STEPS(tbits);
  596.           { const byte *data = bits->data;
  597.             bool full_transfer = (w == w0 && h == h0);
  598.             gx_bitmap_id source_id =
  599.               (full_transfer ? rop_source->id : gx_no_bitmap_id);
  600.               
  601.             if ( source == NULL && lop_no_S_is_T(lop) )
  602.               code = (*dev_proc(pcdev, copy_color))(pcdev,
  603.                 data + bits->raster * yoff, xoff,
  604.                 bits->raster,
  605.                 (full_transfer ? bits->id : gx_no_bitmap_id),
  606.                 x, y, w, h);
  607.             else
  608.               { gx_strip_bitmap data_tile;
  609.  
  610.             data_tile.data = (byte *)data;    /* actually const */
  611.             data_tile.raster = bits->raster;
  612.             data_tile.size.x = data_tile.rep_width =
  613.               ptile->tbits.size.x;
  614.             data_tile.size.y = data_tile.rep_height =
  615.               ptile->tbits.size.y;
  616.             data_tile.id = bits->id;
  617.             data_tile.shift = data_tile.rep_shift = 0;
  618.             code = (*dev_proc(dev, strip_copy_rop))(dev,
  619.                 rop_source->sdata + (y - y0) * rop_source->sraster,
  620.                 rop_source->sourcex + (x - x0),
  621.                 rop_source->sraster, source_id,
  622.                 (rop_source->use_scolors ?
  623.                  rop_source->scolors : NULL),
  624.                 &data_tile, NULL,
  625.                 x, y, w, h,
  626.                 imod(xoff - x, data_tile.rep_width),
  627.                 imod(yoff - y, data_tile.rep_height),
  628.                 lop);
  629.               }
  630.             if ( code < 0 )
  631.               return code;
  632.           }
  633.         END_STEPS
  634.       }
  635.     END_PATTERN_FILL
  636. }
  637. /* Fill a rectangle with an uncolored Pattern. */
  638. /* Note that we treat this as "texture" for RasterOp. */
  639. private int
  640. gx_dc_pure_masked_fill_rect(const gx_device_color *pdevc, int x, int y,
  641.   int w, int h, gx_device *dev, gs_logical_operation_t lop,
  642.   const gx_rop_source_t *source)
  643. {    gx_color_tile *ptile = pdevc->mask.m_tile;
  644.  
  645.     BEGIN_PATTERN_FILL
  646.     if ( ptile == 0 || ptile->is_simple )
  647.         code = (*gx_dc_procs_pure.fill_rectangle)(pdevc, x, y, w, h,
  648.                     pcdev, lop, source);
  649.     else
  650.       {    BEGIN_STEPS(tmask);
  651.         if ( source == NULL )
  652.           code = (*gx_dc_procs_pure.fill_rectangle)(pdevc, x, y, w, h,
  653.                     pcdev, lop, source);
  654.         else
  655.           { gx_rop_source_t step_source;
  656.             SOURCE_STEP(step_source);
  657.             code = (*gx_dc_procs_pure.fill_rectangle)(pdevc,
  658.                     x, y, w, h,
  659.                     pcdev, lop, &step_source);
  660.           }
  661.         if ( code < 0 )
  662.           return code;
  663.         END_STEPS
  664.       }
  665.     END_PATTERN_FILL
  666. }
  667. private int
  668. gx_dc_binary_masked_fill_rect(const gx_device_color *pdevc, int x, int y,
  669.   int w, int h, gx_device *dev, gs_logical_operation_t lop,
  670.   const gx_rop_source_t *source)
  671. {    gx_color_tile *ptile = pdevc->mask.m_tile;
  672.  
  673.     BEGIN_PATTERN_FILL
  674.     if ( ptile == 0 || ptile->is_simple )
  675.         code = (*gx_dc_procs_ht_binary.fill_rectangle)(pdevc,
  676.                     x, y, w, h,
  677.                     pcdev, lop, source);
  678.     else
  679.       {    BEGIN_STEPS(tmask);
  680.         if ( source == NULL )
  681.           code = (*gx_dc_procs_ht_binary.fill_rectangle)(pdevc,
  682.                     x, y, w, h,
  683.                     pcdev, lop, source);
  684.         else
  685.           { gx_rop_source_t step_source;
  686.             SOURCE_STEP(step_source);
  687.             code = (*gx_dc_procs_ht_binary.fill_rectangle)(pdevc,
  688.                     x, y, w, h,
  689.                     pcdev, lop, &step_source);
  690.           }
  691.         if ( code < 0 )
  692.           return code;
  693.         END_STEPS
  694.       }
  695.     END_PATTERN_FILL
  696. }
  697. private int
  698. gx_dc_colored_masked_fill_rect(const gx_device_color *pdevc, int x, int y,
  699.   int w, int h, gx_device *dev, gs_logical_operation_t lop,
  700.   const gx_rop_source_t *source)
  701. {    gx_color_tile *ptile = pdevc->mask.m_tile;
  702.  
  703.     BEGIN_PATTERN_FILL
  704.     if ( ptile == 0 || ptile->is_simple )
  705.         code = (*gx_dc_procs_ht_colored.fill_rectangle)(pdevc,
  706.                     x, y, w, h,
  707.                     pcdev, lop, source);
  708.     else
  709.       {    BEGIN_STEPS(tmask);
  710.         if ( source == NULL )
  711.           code = (*gx_dc_procs_ht_colored.fill_rectangle)(pdevc,
  712.                     x, y, w, h,
  713.                     pcdev, lop, source);
  714.         else
  715.           { gx_rop_source_t step_source;
  716.             SOURCE_STEP(step_source);
  717.             code = (*gx_dc_procs_ht_colored.fill_rectangle)(pdevc,
  718.                     x, y, w, h,
  719.                     pcdev, lop, &step_source);
  720.           }
  721.         if ( code < 0 )
  722.           return code;
  723.         END_STEPS
  724.       }
  725.     END_PATTERN_FILL
  726. }
  727.  
  728. #undef BEGIN_PATTERN_FILL
  729. #undef END_PATTERN_FILL
  730.  
  731. /* Initialize a Pattern color. */
  732. private void
  733. gx_init_Pattern(gs_client_color *pcc, const gs_color_space *pcs)
  734. {    if ( pcs->params.pattern.has_base_space )
  735.     {    const gs_color_space *pbcs =
  736.           (const gs_color_space *)&pcs->params.pattern.base_space;
  737.         cs_init_color(pcc, pbcs);
  738.     }
  739.     /*pcc->pattern = 0;*/        /* cs_full_init_color handles this */
  740. }
  741.  
  742. /* Install a Pattern color space. */
  743. private int
  744. gx_install_Pattern(gs_color_space *pcs, gs_state *pgs)
  745. {    if ( !pcs->params.pattern.has_base_space )
  746.         return 0;
  747.     return (*pcs->params.pattern.base_space.type->install_cspace)
  748.         ((gs_color_space *)&pcs->params.pattern.base_space, pgs);
  749. }
  750.  
  751. /* Adjust the reference counts for Pattern color spaces or colors. */
  752. private void
  753. gx_adjust_cspace_Pattern(const gs_color_space *pcs, gs_memory_t *mem,
  754.   int delta)
  755. {    if ( pcs->params.pattern.has_base_space )
  756.       (*pcs->params.pattern.base_space.type->adjust_cspace_count)
  757.         ((const gs_color_space *)&pcs->params.pattern.base_space, mem, delta);
  758. }
  759.  
  760. private void
  761. gx_adjust_color_Pattern(const gs_client_color *pcc, const gs_color_space *pcs,
  762.   gs_memory_t *mem, int delta)
  763. {    gs_pattern_instance *pinst = pcc->pattern;
  764.  
  765.     if ( pinst != 0 && (pinst->rc.ref_count += delta) == 0 )
  766.       {    /* Release all the storage associated with the instance. */
  767.         gs_state_free(pinst->saved);
  768.         gs_free_object(mem, pinst, "gx_adjust_color_Pattern");
  769.       }
  770.     if ( pcs->params.pattern.has_base_space )
  771.       (*pcs->params.pattern.base_space.type->adjust_color_count)
  772.         (pcc, (const gs_color_space *)&pcs->params.pattern.base_space,
  773.          mem, delta);
  774. }
  775.  
  776. /* GC procedures */
  777.  
  778. #define pcs ((gs_color_space *)vptr)
  779.  
  780. private ENUM_PTRS_BEGIN_PROC(gx_enum_ptrs_Pattern) {
  781.     if ( !pcs->params.pattern.has_base_space )
  782.       return 0;
  783.     return (*pcs->params.pattern.base_space.type->enum_ptrs)
  784.          (&pcs->params.pattern.base_space,
  785.           sizeof(pcs->params.pattern.base_space), index, pep);
  786. } ENUM_PTRS_END_PROC
  787. private RELOC_PTRS_BEGIN(gx_reloc_ptrs_Pattern) {
  788.     if ( !pcs->params.pattern.has_base_space )
  789.       return;
  790.     (*pcs->params.pattern.base_space.type->reloc_ptrs)
  791.       (&pcs->params.pattern.base_space, sizeof(gs_paint_color_space), gcst);
  792. } RELOC_PTRS_END
  793.  
  794. #undef pcs
  795.